<template>
  <div class="app-container">
    <el-form label-width="80px">
      <el-form-item label="信息描述">
        <el-tag type="info"> excel模板说明 </el-tag>
        <el-tag type="success">
          <i class="el-icon-download" />
          <a href="http://localhost:8080/eduservice/subject/excelDownload"
            >点击下载模板</a
          >
        </el-tag>
        <!-- <el-button type="primary" @click="downloadExcel()">下载模板</el-button> -->
      </el-form-item>
      <el-form-item label="选择Excel">
        <!-- 上传 -->
        <el-upload
          class="upload-demo"
          ref="upload"
          action="http://localhost:8080/eduservice/subject/excelUpload"
          :on-remove="handleRemove"
          :file-list="fileList"
          :on-success="handleSuccess"
          :on-error="fileUploadError"
          :auto-upload="false"
          accept="application/vnd.ms-excel"
          :disabled="disabledFlag"
        >
          <el-button slot="trigger" type="primary">选取文件</el-button>
          <el-button
            style="margin-left: 10px"
            type="success"
            @click="submitUpload"
            >上传到服务器</el-button
          >
        </el-upload>
      </el-form-item>
    </el-form>

    <el-form :inline="true" class="demo-form-inline" style="padding: 35px">
      <el-form-item>
        <el-input
          v-model="name"
          placeholder="请输入您要搜索的课程分类"
        ></el-input>
      </el-form-item>
    </el-form>
    <div class="block">
      <el-tree ref="tree" :load="loadNode" :data="treeList" node-key="id" lazy>
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <span>{{ node.label }}</span>
          <span>
            <!-- <el-button
              type="text"
              size="mini"
              @click="() => remove(node, data)"
            >
              删除节点
            </el-button> -->
            <i class="el-icon-delete" @click="() => remove(node, data)"></i>
            <!-- <el-button type="text" @click="() => open(node, data)"
              >修改</el-button        
            > -->
            <i class="el-icon-edit" @click="() => open(node, data)"></i>
          </span>
        </span>
      </el-tree>
    </div>
  </div>
</template>

<script>
import subjectApi from "@/api/subject";
export default {
  data() {
    return {
      name: "",
      fileList: [],
      disabledFlag: false,
      treeList: [],
      props: {
        children: "twoName",
        label: "oneName",
      },
    };
  },
  created() {},
  methods: {
    loadNode(node, resolve) {
      if (node.level == 0) {
        this.getSubject(0, resolve);
      } else {
        this.getSubject(node.data.id, resolve);
      }
    },
    getSubject(parentId, resolve) {
      subjectApi
        .getSubject(parentId)
        .then((res) => {
          resolve(res.data.tree);
        })
        .catch((err) => {});
    },

    downloadExcel() {
      window.location =
        "http://localhost:8080/eduservice/subject/downloadExcel";
    },
    handleSuccess(files, fileList) {
      this.disabledFlag = true;
      this.$message({
        type: "success",
        message: "上传处理成功!",
      });
    },
    submitUpload() {
      this.$refs.upload.submit();
    },
    handleRemove(file, fileList) {
      this.fileList = [];
    },
    fileUploadError(err) {
      this.$message({
        type: "error",
        message: "导入数据失败!",
      });
    },
    remove(node, data) {
      //因为他是异步请求 你放外面了 在请求还没有成功了的时候
      //你就去刷新了但是他检测到数据没有改变  我自然刷新也不会有改变
      //因为网络请求 有网络延迟 要执行 肯定需要1秒钟左右
      //你网络的代码是异步的
      console.log(this.$refs.tree);

      this.$confirm("是否要删除该课程分类信息", "删除课程分类", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        subjectApi
          .deleteSubjectById(data.id)
          .then((res) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });

            //放到这里 没毛病
            node.parent.loaded = false;
            node.parent.expand();
          })
          .catch((err) => {
            this.$message({
              type: "success",
              message: "删除失败!",
            });
            node.parent.loaded = false;
            node.parent.expand();
          });
      });
    },
    open(node, data) {
      this.$prompt("请输入邮箱", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(({ value }) => {
          subjectApi
            .updateSubjectById(data.id, value)
            .then((res) => {
              this.$message({
                type: "success",
                message: "修改成功!",
              });
            node.parent.loaded = false;
            node.parent.expand();

            })
            .catch((err) => {
              this.$message({
                type: "success",
                message: "修改失败!",
              });

            });
        })
        .catch(() => {});
    },
  },
};
</script>

<style>
</style>